package stock.testSB;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import stock.Common;
import stock.StockContrl;
import stock.StockQS;
import stock.model.Stocksb;

public class StockDataUpdater2 implements Runnable {

	public static boolean isTimeInRange() {
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);

		// 将9:15转换为分钟数，9 * 60+15 = 555分钟
		int startTimeInMinutes = 9 * 60 + 15;
		// 将10:00转换为分钟数，10 * 60 = 600分钟
		int endTimeInMinutes = 15 * 60;

		int currentTimeInMinutes = hour * 60 + minute;
		return currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes;
	}

	@Override
	public void run() {
		while (true) {
			if (!isTimeInRange()) {
				System.out.println("当前时间不在9:15到15:00之间");
				return;
			}
			try {
				Thread.sleep(4000);
				LocalDateTime now = LocalDateTime.now();
				DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");
				String timeStr = now.format(formatter);
				String codeall = "";
				for (int i = 0; i < SB1.listall.size(); i++) {
					if (i > 499) {
						break;
					}
					Stock1 stock1 = SB1.listall.get(i);
					codeall += stock1.getName() + ",";
				}
				// System.out.println("-获取数据首板---" + codeall);
				JSONArray jsonall = StockContrl.getCcc(codeall, 1f);
				//System.out.println("-http返回数据首板---" + timeStr);
				for (int i = 0; i < jsonall.size(); i++) {
					String code = jsonall.getJSONObject(i).getString("code");
					SimpleDateFormat sdf = new SimpleDateFormat("mmss");
					Date now1 = new Date();
					String formattedDate = sdf.format(now1);
					TimeZDF tz = new TimeZDF();
					tz.setTime(Long.parseLong(formattedDate));
					DecimalFormat df = new DecimalFormat("0.00");
					String result = df.format(Float.parseFloat(jsonall.getJSONObject(i).getString("zhangdiefu")));
					tz.setZhangdiefu(result);
					for (int j = 0; j < SB1.listall.size(); j++) {
						int temp = 0;
						if (SB1.listall.get(j).getZdfList().size() >= 76) {
							for (int j2 = SB1.listall.get(j).getZdfList().size() - 1; j2 >= 0; j2--) {
								temp++;
								if (temp >= 75) {
									SB1.listall.get(j).getZdfList().remove(j2);
								}
							}
						}
						if (code.equals(SB1.listall.get(j).getName())) {
							SB1.listall.get(j).getZdfList().add(tz);

							break;
						}
					}
				}

				System.out.println("共有" + SB1.listall.size() + "数据计算完成"+timeStr);
				SB1.map1.clear();
				SB1.map3.clear();
				SB1.map5.clear();
				for (int i = 0; i < SB1.listall.size(); i++) {

					DecimalFormat df = new DecimalFormat("0.00");
					// String result = df.format(num);
					if (SB1.listall.get(i).getZdfList().size() <= 0) {
						continue;
					}
					int temp = SB1.listall.get(i).getZdfList().size();
					String cz = SB1.listall.get(i).getZdfList().get(temp - 1).getZhangdiefu();

					if (SB1.listall.get(i).getZdfList().size() >= 15) {
						// System.out.println("aaa");
						String cz1 = SB1.listall.get(i).getZdfList().get(temp - 15).getZhangdiefu();
						Stocksb sk = new Stocksb();
						sk.setCode(SB1.listall.get(i).getName());
						sk.setZdf(Float.parseFloat(cz) - Float.parseFloat(cz1));
						sk.setCzzdf(cz);
						SB1.map1.add(sk);
						// }
					}
					if (SB1.listall.get(i).getZdfList().size() >= 43) {
						String cz3 = SB1.listall.get(i).getZdfList().get(temp - 43).getZhangdiefu();
						Stocksb sk = new Stocksb();
						sk.setCode(SB1.listall.get(i).getName());
						sk.setZdf(Float.parseFloat(cz) - Float.parseFloat(cz3));
						sk.setCzzdf(cz);
						SB1.map3.add(sk);
						// }
					}
					if (SB1.listall.get(i).getZdfList().size() >= 72) {
						String cz5 = SB1.listall.get(i).getZdfList().get(temp - 72).getZhangdiefu();
						Stocksb sk = new Stocksb();
						sk.setCode(SB1.listall.get(i).getName());
						sk.setZdf(Float.parseFloat(cz) - Float.parseFloat(cz5));
						sk.setCzzdf(cz);
						SB1.map5.add(sk);
					}

				}
//				System.out.println("1分钟内涨了5个点的：" + JSONArray.fromObject(SB.map1.size()).toString());
//				System.out.println("3分钟内涨了5个点的：" + JSONArray.fromObject(SB.map3.size()).toString());
//				System.out.println("5分钟内涨了5个点的：" + JSONArray.fromObject(SB.map5.size()).toString());
//				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
